<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
    <head>
        <meta charset="UTF-8">
        <title>menu</title>
        <link rel="stylesheet" href="http://117.80.114.220:81/static/lib/layui/css/layui.css" media="all">
        <link rel="stylesheet" href="http://117.80.114.220:81/static/css/public.css" media="all">
        <style>
            .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
                height: 34px;
                line-height: 34px;
                padding: 0 8px;
            }
        </style>
    </head>
    <body>
        <div class="layuimini-container" sec:authorize="hasAuthority('sys:menu:query')">
            <div class="layuimini-main">
                <div>
                    <div class="layui-btn-group">
                        <button class="layui-btn" id="btn-expand">全部展开</button>
                        <button class="layui-btn" id="btn-fold">全部折叠</button>
                        <button class="layui-btn layui-btn-normal" id="btn-add">新增目录</button>
                    </div>
                    <table id="menu-table" class="layui-table" lay-filter="menu-table"></table>
                </div>
            </div>
        </div>
        <!-- 操作列 -->
        <script type="text/html" id="auth-state">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="add" sec:authorize="hasAuthority('sys:menu:add')">新增</a>
            <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit" sec:authorize="hasAuthority('sys:menu:update')">修改</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" sec:authorize="hasAuthority('sys:menu:delete')">删除</a>
        </script>

        <script src="http://117.80.114.220:81/static/lib/layui/layui.js" charset="utf-8"></script>
        <script src="http://117.80.114.220:81/static/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
        <script src="http://117.80.114.220:81/static/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
        <script src="http://117.80.114.220:81/static/js/my.js"></script>
        <script>
            layui.use(['table', 'treetable'], function () {
                var $ = layui.jquery;
                var table = layui.table;
                var treetable = layui.treetable;
                var baseURL = layui.baseUrl;

                // 渲染表格
                layer.load(2);
                treetable.render({
                    treeColIndex: 1,
                    treeSpid: -1,
                    treeIdName: 'id',
                    treePidName: 'pid',
                    elem: '#menu-table',
                    url: baseURL + '/sys-menu/list',
                    page: false,
                    cols: [
                        [
                            {type: 'numbers'},
                            {field: 'name', minWidth: 200, title: '菜单名称'},
                            {field: 'authority', title: '权限标识'},
                            {field: 'menuUrl', title: '菜单url'},
                            {field: 'orderNum', width: 80, align: 'center', title: '排序号'},
                            {
                                field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                                    if (d.isMenu == 1) {
                                        return '<span class="layui-badge layui-bg-gray">按钮</span>';
                                    }
                                    if (d.pid == -1) {
                                        return '<span class="layui-badge layui-bg-blue">目录</span>';
                                    } else {
                                        return '<span class="layui-badge-rim">菜单</span>';
                                    }
                                }, title: '类型'
                            },
                            {templet: '#auth-state', width: 200, align: 'center', title: '操作'}
                        ]
                    ],
                    done: function () {
                        layer.closeAll('loading');
                    }
                });

                $('#btn-expand').click(function () {
                    treetable.expandAll('#menu-table');
                });

                $('#btn-fold').click(function () {
                    treetable.foldAll('#menu-table');
                });

                $('#btn-add').click(function () {
                    var index = layer.open({
                        title: '新增用户',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: 'add.html'
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                });

                //监听工具条
                table.on('tool(menu-table)', function (obj) {
                    var data = obj.data;
                    var layEvent = obj.event;

                    if (layEvent === 'add') {
                        var index = layer.open({
                            title: '新增菜单',
                            type: 2,
                            shade: 0.2,
                            maxmin: true,
                            shadeClose: true,
                            area: ['100%', '100%'],
                            content: 'add.html?pid=' + data.id,
                        });
                        $(window).on("resize", function () {
                            layer.full(index);
                        });
                    } else if (layEvent === 'del') {
                        myGet("/sys-menu/findMenuByPid?pid=" + data.id, null, function (resp) {
                            if (resp.code == 0) {
                                var msg;
                                if (resp.data) {
                                    msg = '该菜单下有其他子菜单，是否一并删除？';
                                } else {
                                    msg = '真的删除行么';
                                }
                                layer.confirm(msg, function (index) {
                                    var idJson = JSON.stringify({id: data.id});
                                    myPost("/sys-menu/deleteById", idJson, function (data) {
                                        if (data.code == 0) {
                                            layer.msg('删除成功', {icon: 1});
                                            location.reload();
                                        } else {
                                            layer.msg(data.msg);
                                        }
                                    });
                                    layer.close(index);
                                });
                            } else {
                                layer.msg(resp.msg);
                            }
                        });
                    } else if (layEvent === 'edit') {
                        var index = layer.open({
                            title: '修改菜单',
                            type: 2,
                            shade: 0.2,
                            maxmin: true,
                            shadeClose: true,
                            area: ['100%', '100%'],
                            content: 'edit.html?id=' + data.id,
                        });
                        $(window).on("resize", function () {
                            layer.full(index);
                        });
                    }
                });
            });
        </script>
    </body>
</html>
